﻿using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.Text;

namespace DocMark
{
    internal class WordHandler : IMarkLister
    {
        public event Action<string>? HandleMsg;

        public async Task<List<string>> GetTextAsync(string filePath, Color textColor)
        {
            using var stream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite);
            return await GetTextAsync(stream, textColor);
        }

        public async Task<List<string>> GetTextAsync(Stream stream, Color textColor)
        {
            return await Task.Run(() =>
            {
                Document document = new(stream);
                List<string> finsStrs = new();
                StringBuilder stringBuilder = new();
                foreach (Section section in document.Sections)
                {
                    foreach (Paragraph paragraph in section.Paragraphs)
                    {
                        for (int i = 0; i < paragraph.CharCountIncludeSpace - 1; i++)
                        {
                            TextSelection textSelection = new(paragraph, i, i + 1);
                            TextRange textRange = textSelection.GetAsOneRange();
                            if (textRange.CharacterFormat.TextColor == textColor)
                            {
                                stringBuilder.Append(textRange.Text);
                            }
                            else if (stringBuilder.Length > 0)
                            {
                                string msg = stringBuilder.ToString().TrimEnd().TrimStart();
                                finsStrs.Add(msg);
                                stringBuilder.Clear();
                            }
                        }
                        if (stringBuilder.Length > 0)
                        {
                            finsStrs.Add(stringBuilder.ToString().TrimEnd().TrimStart());
                            stringBuilder.Clear();
                        }
                    }
                }
                document.Close();
                document.Dispose();
                return finsStrs;
            });
        }

        public async Task<bool> MarkAsync(string filePath, string saveFilePath, List<string> markTexts, Color textColor)
        {
            return await Task.Run(() =>
            {
                Document document = new(filePath);
                foreach (var targetStr in markTexts)
                {
                    TextRange? textRange = document.FindString(targetStr, true, false)?.GetAsOneRange();
                    if (textRange == null)
                    {
                        HandleMsg?.Invoke(SimpleLog.Warring($"未找到 {targetStr}"));
                        continue;
                    }
                    textRange.CharacterFormat.TextColor = textColor;
                }
                document.SaveToFile(saveFilePath);
                document.Close();
                document.Dispose();
                return true;
            });
        }
    }
}
